Skip to main content

@pexip/peer-connection

Enumerations

Interfaces

Type Aliases

DetachFn

Ƭ DetachFn: ReturnType<Signal<unknown>["add"]>


EventHandler

Ƭ EventHandler: (event: Event) => void

Type declaration

▸ (event): void

Parameters
NameType
eventEvent
Returns

void


GetSignalTypeFromInterface

Ƭ GetSignalTypeFromInterface<T, K>: T[K] extends Signal<infer S> ? S : never

Type parameters

NameType
TT
Kextends keyof T

LogFn

Ƭ LogFn: <T>(msg: string, context?: T) => void

Type declaration

▸ <T>(msg, context?): void

Type parameters
NameType
Textends Record<string, unknown>
Parameters
NameType
msgstring
context?T
Returns

void


MainPeerConnection

Ƭ MainPeerConnection: BasePeerConnection


MainPeerConnectionOptions

Ƭ MainPeerConnectionOptions: PeerConnectionOptions


MediaConfig

Ƭ MediaConfig: TransceiverConfig | DataChannelConfig


MediaDirection

Ƭ MediaDirection: Exclude<RTCRtpTransceiverDirection, "stopped">


MediaInit

Ƭ MediaInit: TransceiverInit | DataChannelInit


MediaType

Ƭ MediaType: "application" | "text" | "message" | TransceiverMediaType

Media Types ("media")

They are "audio", "video", "text", "application", and "message"

See

https://www.rfc-editor.org/rfc/rfc4566#section-8.2.1


OnDataChannelEventHandler

Ƭ OnDataChannelEventHandler: (event: RTCDataChannelEvent) => void

Type declaration

▸ (event): void

Parameters
NameType
eventRTCDataChannelEvent
Returns

void


OnIceCandidateHandler

Ƭ OnIceCandidateHandler: (event: RTCPeerConnectionIceEvent) => void

Type declaration

▸ (event): void

Parameters
NameType
eventRTCPeerConnectionIceEvent
Returns

void


OnNegotiationNeededHandler

Ƭ OnNegotiationNeededHandler: () => void

Type declaration

▸ (): void

Returns

void


OnRemoteStreamsEventHandler

Ƭ OnRemoteStreamsEventHandler: (config: TransceiverConfig) => void

Type declaration

▸ (config): void

Parameters
NameType
configTransceiverConfig
Returns

void


OnSecureCheckCodeHandler

Ƭ OnSecureCheckCodeHandler: (secureCheckCode: string) => void

Type declaration

▸ (secureCheckCode): void

Parameters
NameType
secureCheckCodestring
Returns

void


OnTrackEventHandler

Ƭ OnTrackEventHandler: (event: RTCTrackEvent) => void

Type declaration

▸ (event): void

Parameters
NameType
eventRTCTrackEvent
Returns

void


OnTransceiverChangeHandler

Ƭ OnTransceiverChangeHandler: () => void

Type declaration

▸ (): void

Returns

void


PCOptionalsSignals

Ƭ PCOptionalsSignals: Partial<PeerConnectionSignals> & Pick<PeerConnectionCommandSignals, "onReceiveIceCandidate">


PCRequiredSignals

Ƭ PCRequiredSignals: CorePeerConnectionSignals & Omit<PeerConnectionCommandSignals, "onReceiveIceCandidate">


PCSignals

Ƭ PCSignals: PCRequiredSignals & PCOptionalsSignals


RTCPeerConnectionEventListeners

Ƭ RTCPeerConnectionEventListeners: EventListeners<RTCPeerConnection, RTCPeerConnectionEventMap, keyof RTCPeerConnectionEventMap>


ReferenceValue

Ƭ ReferenceValue: string


References

Ƭ References: Record<string, ReferenceValue>


TransceiverConfigDirectionTuple

Ƭ TransceiverConfigDirectionTuple: [TransceiverConfig, (MediaDirection | "send" | "recv")?]

Variables

REQUIRED_SIGNAL_KEYS

Const REQUIRED_SIGNAL_KEYS: readonly ["onOfferRequired", "onReceiveAnswer", "onReceiveOffer", "onOffer", "onOfferIgnored", "onAnswer", "onError"]

Functions

assertTransceiverMediaType

assertTransceiverMediaType(mediaType): "audio" | "video"

Parameters

NameType
mediaTypeunknown

Returns

"audio" | "video"


changeTransceiverDirection

changeTransceiverDirection(currentDirection, intendedDirection?): MediaDirection

Try to derive the direction from currentDirection and the intendedDirection

Parameters

NameTypeDefault value
currentDirectionRTCRtpTransceiverDirectionundefined
intendedDirectionMediaDirection | "send" | "recv"'send'

Returns

MediaDirection


compareArray

compareArray<T>(list1, list2, predicate): boolean

Compare 2 Array and using the provided predicate to compare

Type parameters

Name
T

Parameters

NameType
list1undefined | T[]
list2undefined | T[]
predicate(a: undefined | T, b: undefined | T) => boolean

Returns

boolean


compareRecord

compareRecord<T>(record1, record2): boolean

Compare 2 flat records (an object with primitive type) using Object.is comparator

Remarks

No deep comparison

Type parameters

NameType
Textends object

Parameters

NameType
record1undefined | T
record2undefined | T

Returns

boolean


createEventQueue

createEventQueue<T>(callback): Object

Create a queue to handle buffering and trigger provided callback being called in order.

Type parameters

Name
T

Parameters

NameTypeDescription
callback(item: T) => voidThe callback being called

Returns

Object

NameType
discard() => void
enqueue(item: T) => void
flush() => T[]
get buffering()boolean
get items()T[]
get length()number

createGetRefs

createGetRefs(pc): () => { answerOptions: undefined | RTCAnswerOptions = pc.answerOptions; connectionState: RTCPeerConnectionState ; iceConnectionState: RTCIceConnectionState ; iceGatheringState: RTCIceGatheringState ; module: undefined | string = refs.module; offerOptions: undefined | RTCOfferOptions = pc.offerOptions; references: References = refs; signalingState: RTCSignalingState }

Parameters

NameType
pcPeerConnection

Returns

fn

▸ (): Object

Returns

Object

NameType
answerOptionsundefined | RTCAnswerOptions
connectionStateRTCPeerConnectionState
iceConnectionStateRTCIceConnectionState
iceGatheringStateRTCIceGatheringState
moduleundefined | string
offerOptionsundefined | RTCOfferOptions
referencesReferences
signalingStateRTCSignalingState

createMainPeerConnection

createMainPeerConnection(signals, options?): MainPeerConnection

Logical layer of the Peer Connection for Call/Main, which connecting Signals and RTCPeerConnection events,

See

PCSignals

Parameters

NameTypeDescription
signalsPCSignalsProvide the required signals for communication
optionsPeerConnectionOptionsConfiguration for the the Peer Connection

Returns

MainPeerConnection


createMediaConfigs

createMediaConfigs(mediaInits?, onTransceiverChanged?): Object

Parameters

NameType
mediaInits?MediaInit[]
onTransceiverChanged?OnTransceiverChangeHandler

Returns

Object

NameType
addConfig(peer: RTCPeerConnection, initOrConfig: DataChannelConfig | DataChannelInit) => DataChannelConfig(peer: RTCPeerConnection, initOrConfig: TransceiverConfig | TransceiverInit) => TransceiverConfig
find(predicate: (config: TransceiverConfig | DataChannelConfig) => boolean) => undefined | TransceiverConfig | DataChannelConfig
getConfig(key: RTCRtpTransceiver) => TransceiverConfig | undefined(key: RTCDataChannel) => DataChannelConfig | undefined
get configs()(TransceiverConfig | DataChannelConfig)[]

createPCSignal

createPCSignal<T>(name, crucial?): Signal<T>

Create a general signal with consistent scoped name

Type parameters

NameType
Tundefined

Parameters

NameTypeDefault valueDescription
namestringundefinedSignal name
crucialbooleantrueSignify if the signal is unmissable.

Returns

Signal<T>


createPCSignals

createPCSignals<K>(more, scope?): Pick<Signals, SignalKeys>

Create and return all required and optional (if specified with more), signals for peer connection to work

See

  • PCOptionalsSignals
  • Signal

The following signals created by default 'onOfferRequired', 'onReceiveAnswer', 'onReceiveOffer', 'onOffer', 'onAnswer', 'onError',

  • REQUIRED_SIGNAL_KEYS

Type parameters

NameType
Kextends "onReceiveIceCandidate" | keyof PeerConnectionSignals

Parameters

NameTypeDefault valueDescription
moreK[]undefinedKeys from PCOptionalsSignals,
scopestring''any scope prefix for the generated signal name,

Returns

Pick<Signals, SignalKeys>


createPeerConnection

createPeerConnection(options?, peerConnection?): PeerConnection

Wrap RTCPeerConnection with polyfill the old APIs and simplifies the common logics

Parameters

NameTypeDescription
optionsPeerConnectionOptionsConfiguration for the peer connection
peerConnection?ExtendedRTCPeerConnectionInject your own RTCPeerConnection, can be used for testing

Returns

PeerConnection


createRTCPeerConnection

createRTCPeerConnection(options?, listeners?, existingPC?): ExtendedRTCPeerConnection

A wrapper to create a RTCPeerConnection instance and subscribe the events declared from the provided list of listeners.

Parameters

NameTypeDescription
options?RTCConfigurationThe configuration for the RTCPeerConnection
listeners?RTCPeerConnectionEventListenersA list of listeners to listen on the Peer Connection events
existingPC?RTCPeerConnectionInstead of creating a new Peer Connection, it always uses this instance instead.

Returns

ExtendedRTCPeerConnection


createRefsLog

createRefsLog(getRefs): Object

create a logger with reference attached

Parameters

NameTypeDescription
getRefs() => Record<string, unknown>A function to the references for logging

Returns

Object

NameType
debugLogFn
errorLogFn
infoLogFn
warnLogFn

deriveSendDirectionFromTrack

deriveSendDirectionFromTrack(intendedDirection, track): MediaDirection

Parameters

NameType
intendedDirectionMediaDirection
trackundefined | null | MediaStreamTrack

Returns

MediaDirection


getCreateLoopbackConnectionFn

getCreateLoopbackConnectionFn(rtcConnection?, rtcLoopbackConnection?, loopbackStream?): (stream: MediaStream) => Promise<MediaStream>

workaround to allow echo cancellation in Chromium browsers, due to https://bugs.chromium.org/p/chromium/issues/detail?id=687574.

based on https://dev.to/focusedlabs/echo-cancellation-with-web-audio-api-and-chromium-1f8m and https://gist.github.com/alexciarlillo/4b9f75516f93c10d7b39282d10cd17bc

Parameters

NameType
rtcConnectionRTCPeerConnection
rtcLoopbackConnectionRTCPeerConnection
loopbackStreamMediaStream

Returns

fn

▸ (stream): Promise<MediaStream>

Parameters
NameType
streamMediaStream
Returns

Promise<MediaStream>


getPeerConnectionStates

getPeerConnectionStates(pc): Object

Get the states from RTCPeerConnection

Parameters

NameTypeDescription
pcExtendedRTCPeerConnection | PeerConnectionthe peer connection to get the states

Returns

Object

NameType
get connectionState()RTCPeerConnectionState
get iceConnectionState()RTCIceConnectionState
get iceGatheringState()RTCIceGatheringState
get signalingState()RTCSignalingState

getRelativeDirection

getRelativeDirection(remoteDirection): MediaDirection

Get the relative direction so you can get the correct direction, e.g. "sendonly" <--> "recvonly"

From https://www.rfc-editor.org/rfc/rfc3264.html#section-6.1 > If a stream is offered as sendonly, the corresponding stream MUST be > marked as recvonly or inactive in the answer. If a media stream is > listed as recvonly in the offer, the answer MUST be marked as > sendonly or inactive in the answer. If an offered media stream is > listed as sendrecv (or if there is no direction attribute at the > media or session level, in which case the stream is sendrecv by > default), the corresponding stream in the answer MAY be marked as > sendonly, recvonly, sendrecv, or inactive. If an offered media > stream is listed as inactive, it MUST be marked as inactive in the > answer.

Parameters

NameType
remoteDirectionundefined | MediaDirection

Returns

MediaDirection


getStatesAndProps

getStatesAndProps(pc): Object

Get common states and props from PeerConnection

Parameters

NameTypeDescription
pcPeerConnectionThe peer connection to get the info

Returns

Object

NameType
answerOptionsundefined | RTCAnswerOptions
connectionStateRTCPeerConnectionState
iceConnectionStateRTCIceConnectionState
iceGatheringStateRTCIceGatheringState
offerOptionsundefined | RTCOfferOptions
signalingStateRTCSignalingState

isDataChannelConfig

isDataChannelConfig(t): t is DataChannelConfig

Parameters

NameType
tunknown

Returns

t is DataChannelConfig


isDataChannelInit

isDataChannelInit(t): t is DataChannelInit

Parameters

NameType
tunknown

Returns

t is DataChannelInit


isMediaConfig

isMediaConfig(t): t is MediaConfig

Parameters

NameType
tunknown

Returns

t is MediaConfig


isMediaInit

isMediaInit(t): t is MediaInit

Parameters

NameType
tunknown

Returns

t is MediaInit


isMediaType

isMediaType(t): t is "audio" | "video" | "application"

Parameters

NameType
tunknown

Returns

t is "audio" | "video" | "application"


isSameStream

isSameStream(stream1?, stream2?): undefined | boolean

Compare the provided 2 streams to check if they are the same

Parameters

NameTypeDescription
stream1?MediaStreamthe stream to compare
stream2?MediaStreamthe stream to compare

Returns

undefined | boolean


isTransceiverConfig

isTransceiverConfig(t): t is TransceiverConfig

Parameters

NameType
tunknown

Returns

t is TransceiverConfig


isTransceiverInit

isTransceiverInit(t): t is TransceiverInit

Parameters

NameType
tunknown

Returns

t is TransceiverInit


isTransceiverMediaType

isTransceiverMediaType(mediaType): mediaType is "audio" | "video"

Parameters

NameType
mediaTypeunknown

Returns

mediaType is "audio" | "video"


isTransceiverObsolete

isTransceiverObsolete(transceiver): boolean

Parameters

NameType
transceiverRTCRtpTransceiver

Returns

boolean


logReferences

logReferences(refs): Object

Parameters

NameType
refsReferences

Returns

Object

NameType
moduleundefined | string
referencesReferences

merge

merge<T>(oldParam, newParam): RTCRtpEncodingParameters[]

Merge two array and overwriting the old on with the new one in order

Type parameters

NameType
Textends object[]

Parameters

NameType
oldParamT
newParamT

Returns

RTCRtpEncodingParameters[]


resolveKindOrTrack

resolveKindOrTrack(kindOrTrack): Pick<TransceiverConfig, "track" | "kind">

Parameters

NameType
kindOrTrackundefined | "audio" | "video" | MediaStreamTrack

Returns

Pick<TransceiverConfig, "track" | "kind">


setLogger

setLogger(newLogger): void

Parameters

NameType
newLoggerLogger

Returns

void


subscribePCEvents

subscribePCEvents(pc, listeners): void

Parameters

NameType
pcRTCPeerConnection
listenersRTCPeerConnectionEventListeners

Returns

void


wirePeerConnectionEventHandler

wirePeerConnectionEventHandler<T>(wireOptions): void

Wire the peer connection event with the pre-defined handler and signal accordingly

Type parameters

NameType
Textends keyof PeerConnectionSignals

Parameters

NameTypeDescription
wireOptionsObjectWire event params + key - event key + pc - peer connection + signal - the signal to wire
wireOptions.keyT-
wireOptions.pcPeerConnection-
wireOptions.signalPeerConnectionSignals[T]-

Returns

void


wirePeerConnectionEvents

wirePeerConnectionEvents(pc, signals): void

Wire the peer connection events with provided signals

Parameters

NameTypeDescription
pcPeerConnectionpeer connection
signalsPartial<PeerConnectionSignals>the set of signals to wire

Returns

void


withSignals

withSignals(peer): (signals: Omit<PCSignals, "onOfferRequired">) => Detach[]

Handle some core signals for the peer connection.

Parameters

NameTypeDescription
peerPeerConnectionThe peer connection

Returns

fn

the signal subscriptions which are needed to be called when closing the peer connection

▸ (signals): Detach[]

Map signals

Parameters
NameTypeDescription
signalsOmit<PCSignals, "onOfferRequired">The provided signals to map to the PC events
Returns

Detach[]